﻿using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Services;
using Abp.Domain.Repositories;
using FwProject.Core.Car;
using System.Threading.Tasks;
using System.Linq;
using Abp.UI;
namespace FwProject.Manager
{
   public class CarInfoManager:IDomainService
    {
        private readonly IRepository<CarInfo, Guid> _carRepository;
        public CarInfoManager(IRepository<CarInfo, Guid> carRepository) {
            _carRepository = carRepository;

        }
        /// <summary>
        /// 添加一个配送汽车
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public async Task<Guid> CreateCar(CarInfo car) {

           var model= _carRepository.GetAll().Where(o => o.PlatesNumber == car.PlatesNumber && o.EmployeeCarUserId==car.EmployeeCarUserId && o.FactoryinfoId==car.FactoryinfoId).FirstOrDefault();
            if (model != null)
                throw new UserFriendlyException("当前汽车已存在");

          return  await _carRepository.InsertAndGetIdAsync(car);
        }
        /// <summary>
        ///修改一个配送汽车
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public async Task UpdateCar(CarInfo car)
        {
        
             await _carRepository.UpdateAsync(car);
        }
        /// <summary>
        /// 返回一个车辆信息
        /// </summary>
        /// <returns>The by identifier.</returns>
        /// <param name="PlatesNumber">Plates number.</param>
        public CarInfo GetById(string PlatesNumber)
        {

            var model =  _carRepository.GetAllIncluding(o => o.CarUser).Where(o => o.PlatesNumber == PlatesNumber).FirstOrDefault();
          
            if (model == null)
                throw new UserFriendlyException("该车辆不存在");

                return model;
        }
        /// <summary>
       ///  delete 一个配送汽车
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public async Task DeleteCar(CarInfo car)
        {

            await _carRepository.DeleteAsync(car);
        }

    }
}

